package leetcode;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Ghost on 2014/12/6.
 */
public class Letter_Combinations_of_a_Phone_Number {
    static HashMap<Character, String> map = new HashMap<Character, String>();

    static {
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
    }

    private List<String> rst = new LinkedList<String>();
    private StringBuffer sb = new StringBuffer("");

    private void comb(String digits) {

        char d = digits.charAt(0);
        char[] dStr = map.get(d).toCharArray();

        if (digits.length() == 1) {
            for (char c : dStr) {
                sb.append(c);
                rst.add(sb.toString());
                sb.deleteCharAt(sb.length() - 1);
            }
        } else
            for (char c : dStr) {
                sb.append(c);
                comb(digits.substring(1));
                sb.deleteCharAt(sb.length() - 1);
            }
    }

    public List<String> letterCombinations(String digits) {
        if(digits==null)
            return rst;
        if(digits.isEmpty())
            rst.add("");
        comb(digits);
        return rst;
    }

    public static void main(String[] args){
        Letter_Combinations_of_a_Phone_Number letter_combinations_of_a_phone_number = new Letter_Combinations_of_a_Phone_Number();
        System.out.println(letter_combinations_of_a_phone_number.letterCombinations("2"));
    }
}
